/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonegap.plugin;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoViewDemo extends Activity {
	
	private static final String TAG = "VideoViewDemo";
	private String MEDIA_NAME = "";
	
	private VideoView mVideoView;
	private EditText mPath;
	private ImageButton mPlay;
	private ImageButton mPause;
	private ImageButton mReset;
	private ImageButton mStop;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
//		setContentView(R.layout.main);
		setContentView(R.layout.mediaplyer);
		MEDIA_NAME = this.getIntent().getStringExtra("name");
		Log.i("getExtra", "this.getIntent().getStringExtra is " + MEDIA_NAME);
		
		mVideoView = (VideoView) findViewById(R.id.surface_view);
		mVideoView.setMediaController(new MediaController(this));

		mPath = (EditText) findViewById(R.id.path);
		mPath.setText("http://daily3gp.com/vids/747.3gp");

		mPlay = (ImageButton) findViewById(R.id.play);
		mPause = (ImageButton) findViewById(R.id.pause);
		mReset = (ImageButton) findViewById(R.id.reset);
		mStop = (ImageButton) findViewById(R.id.stop);
		
		mPlay.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				playVideo(MEDIA_NAME);
			}
		});
		mPause.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				if (mVideoView != null) {
					mVideoView.pause();
				}
			}
		});
		mReset.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				if (mVideoView != null) {
					mVideoView.seekTo(0);
				}
			}
		});
		mStop.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				if (mVideoView != null) {
					mVideoView.stopPlayback();
				}
			}
		});		
		runOnUiThread(new Runnable(){
			public void run() {				
				playVideo(MEDIA_NAME);				
			}			
		});
	}

	private void playVideo(String name) {
		try {			
			//mVideoView.setVideoURI(Uri.parse(urlPath));
			mVideoView.setVideoPath(name);
			mVideoView.start();
			mVideoView.requestFocus();
		} catch (Exception e) {
			Log.e(TAG, "error: " + e.getMessage(), e);
			if (mVideoView != null) {
				mVideoView.stopPlayback();
			}
		}
	}
}
